package com.achievement.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.achievement.domain.dto.PaperIncludeDTO;
import com.achievement.domain.entity.PaperIncludeEntity;
import com.achievement.domain.vo.PaperIncludeVO;
import com.achievement.mapper.PaperIncludeMapper;
import com.achievement.mappings.paper.PaperMapping;
import com.achievement.service.IAchievementFileService;
import com.achievement.service.IPaperIncludeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.api.CommonFileApi;
import com.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author CNKI
* @description 针对表【biz_paper_include(科研成果-学术论文-论文编辑-论文收录)】的数据库操作Service实现
* @createDate 2025-02-17 17:06:19
*/
@Service
public class PaperIncludeServiceImpl extends ServiceImpl<PaperIncludeMapper, PaperIncludeEntity>
    implements IPaperIncludeService {

    @Autowired
    private PaperMapping mapping;

    @Autowired
    private IAchievementFileService fileService;

    //通用文件api
    @Autowired
    private CommonFileApi fileApi;


    /**
     * 新增论文收录信息
     *
     * @param dto dto
     * @return boolean
     */
    @Override
    public boolean addPaperInclude(PaperIncludeDTO dto) {
        if (ObjectUtil.isNull(dto)){
            return true;
        }

        String UUID = IdUtil.randomUUID().replace("-", "");

        dto.setId(UUID);

        PaperIncludeEntity PaperIncludeEntity = mapping.PaperIncludeDto2Entity(dto);

        return this.save(PaperIncludeEntity);
    }

    /**
     * 编辑论文收录信息
     *
     * @param dto dto
     * @return boolean
     */
    @Override
    public boolean editPaperInclude(PaperIncludeDTO dto) {
        PaperIncludeEntity PaperIncludeEntity = mapping.PaperIncludeDto2Entity(dto);

        return  this.updateById(PaperIncludeEntity);
    }

    /**
     * 删除论文收录信息
     *
     * @param id id
     * @return boolean
     */
    @Override
    public boolean delPaperInclude(String id) {

        PaperIncludeEntity paperIncludeEntity = this.baseMapper.selectById(id);

        if (paperIncludeEntity != null && StringUtils.isNotBlank(paperIncludeEntity.getEmbodyFileId())) {
            // 删除文件
            fileApi.delFile(paperIncludeEntity.getEmbodyFileId());
        }

        return this.removeById(id);
    }

    /**
     * 根据论文id，查询论文收录信息列表
     * @param id id
     * @return List<PaperIncludeVO>
     */
    @Override
    public List<PaperIncludeVO> getInfoById(String id) {
        LambdaQueryWrapper<PaperIncludeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaperIncludeEntity::getPaperId,id);
        List<PaperIncludeEntity> list = this.list(queryWrapper);

        List<PaperIncludeVO> voList = list.stream().map(mapping::PaperIncludeEntity2Vo).collect(Collectors.toList());

        for (PaperIncludeVO paperIncludeVO : voList) {
            //附件信息
            String fileIds = paperIncludeVO.getEmbodyFileId();
            if (StringUtils.hasText(fileIds)) {
                String[] fileIdArr = fileIds.split(",");
                List<Map<String,String>> fileInfo = fileService.getFileInfoByFileIds(fileIdArr);
                paperIncludeVO.setFileList(fileInfo);
            }
        }


        return voList;
    }
}




